import {Injectable} from '@angular/core';
import {HttpService} from "@core/http/http.service";
import * as API from "@api/index";
import {Observable} from "rxjs";
import {ToolsService} from "@shared/service/tools/tools.service";
import {Response} from "@angular/http";
import {
  CommonPageVo,
  CrowdDetailResp,
  CrowdDynamicRespVO,
  CrowdDynamicSaveReq,
  CrowdSupporterResp,
  SNSResult
} from "@interface/index";


@Injectable()
export class CrowdService {

  constructor(
    public httpService: HttpService,
    public toolsService: ToolsService
  ) {
  }

  // 查询众筹列表
  getCrowdList(param) {
    const url = param.userId ? API.CROWD_LIST + '?userId=' + param.userId +
      `&page=${param.page}&rows=${param.rows}` : API.CROWD_LIST + `?page=${param.page}&rows=${param.rows}`;
    return this.httpService.get(url).map((res) => res.json());
  }

  // 查询已登录用户当日是否可以继续发起众筹
  getCanAdd() {
    const url = API.CROWD_CAN_ADD;
    return this.httpService.get(url).map((res) => res.json());
  }


  // 众筹详情接口
  getCrowdDetail(crowdCode: string, userId?: string): Observable<SNSResult<CrowdDetailResp>> {
    let url;
    url = userId ? (API.CROWD_DETAIL + '?crowdCode=' + crowdCode +
      '&supportUserId=' + userId) : API.CROWD_DETAIL + '?crowdCode=' + crowdCode;
    return this.httpService.get(url).map((res) => res.json());
  }

  // 获取众筹sku列表
  getSkuInfo(crowdCode, goodsCode) {
    const url = API.CROWD_SKU + '?crowdCode=' + crowdCode + `&goodsCode=` + goodsCode;
    return this.httpService.get(url).map((res) => res.json());
  }

  // 获取活动类型
  getActiveType() {
    const url = API.CROWD_ACTIVE_TYPE + '?parentValue=0&dictType=50';
    return this.httpService.get(url).map((res) => res.json());
  }

  // 获取运动类型
  getSportType() {
    const url = API.CROWD_ACTIVE_TYPE + '?parentValue=3&dictType=3';
    return this.httpService.get(url).map((res) => res.json());
  }

  // 获取草稿信息
  getCrowdInfo(data?) {
   /* let url;
    data ? url = API.CROWD_GETCROWDINFO + '?crowdCode=' + data : url = API.CROWD_GETCROWDINFO;*/

    let url = data ? API.CROWD_GETCROWDINFO + '?crowdCode=' + data : API.CROWD_GETCROWDINFO;
    return this.httpService.get(url).map((res) => res.json());
  }

  // 保存众筹发起第一步
  saveOne(data) {
    const url = API.CROWD_SAVE_ONE;
    return this.httpService.postJsonUTF(url, data).map((res) => res.json());
  }

  // 保存众筹发起第二步
  saveTwo(data) {
    const url = API.CROWD_SAVE_TWO;
    return this.httpService.postJsonUTF(url, data).map((res) => res.json());
  }

  // 保存众筹发起第三步
  saveThree(data) {
    const url = API.CROWD_SAVE_THREE;
    return this.httpService.postJsonUTF(url, data).map((res) => res.json());
  }

  //  上传图片
  updateAvatar(file) {
    const url = API.UPLOAD_IMAGE;
    //  let url = API.AVATAR_URL;
    return this.httpService.upload(url, file).map((res: Response) => res.json());
  }

  // 处理时间戳
  dealTimeStamp(date) {
    let dealTime = date.split('T');
    dealTime = dealTime[0] + ' ' + dealTime[1];
    let safariTime = dealTime.split('-');
    safariTime = safariTime[0] + '/' + safariTime[1] + '/' + safariTime[2];
    const safariStamp = safariTime.split('.')[0];
    return new Date(safariStamp).getTime();
  }

  /**
   * @description 众筹支持者
   * @author @zhupw1360
   * @date 2017-11-14
   * @type {string}
   */
  getCrowdSupporters(params): Observable<SNSResult<CommonPageVo<CrowdSupporterResp>>> {
    const url = API.CROWD_SUPPORTERS + "?" + this.toolsService.param(params);
    return this.httpService.get(url).map((res) => res.json());
  }

  // 获取活动动态 列表
  getCrowdDynamics(params): Observable<SNSResult<CommonPageVo<CrowdDynamicRespVO>>> {
    const url = API.CROWD_DYNAMICS + "?" + this.toolsService.param(params);
    return this.httpService.get(url).map((res) => res.json());
  }

  // 发布活动动态
  setCrowdDynamics(data): Observable<SNSResult<CrowdDynamicSaveReq>> {
    const url = API.CROWD_SAVE_DYNAMIC;
    return this.httpService.postJson(url, data).map(res => res.json());
  }

  //  我发起的众筹列表
  getMyCrowdList(param) {
    const url = API.CROWD_LIST + '?userId=' + param.userId + `&page=${param.page}&rows=${param.rows}`;
    return this.httpService.get(url).map((res) => res.json());
  }

  // 我参与的众筹列表
  getMyAttendList(params) {
    const url = API.CROWD_ATTEND_LIST + '?' + this.toolsService.param(params);
    return this.httpService.get(url).map((res: Response) => res.json());
  }

  // 删除活动动态
  delCrowdDynamics(id: number) {
    const url = API.CROWD_DEL_DYNAMICS + `?id=${id}`;
    return this.httpService.delete(url).map(res => res.json());
  }

  // （已下架）删除众筹
  delOutCrowd(crowdCode) {

    //  console.log(typeof(crowdCode));
    const url = API.CROWD_DEL + `?crowdCode=${crowdCode}`;
    return this.httpService.delete(url).map(res => res.json());
  }

  // 提前结束
  completeCrowd(data) {
    const url = API.CROWD_COMPLETE;
    return this.httpService.putUrlencode(url, data).map((res: Response) => res.json());
  }

  // 上架
  onRackCrowd(data) {
    const url = API.CROWD_ONRACKCROWD;
    return this.httpService.putUrlencode(url, data).map((res: Response) => res.json());
  }

  // 下架
  offRackCrowd(data) {
    const url = API.CROWD_OFFRACK;
    return this.httpService.putUrlencode(url, data).map((res: Response) => res.json());
  }
}
